﻿using System;
using System.Collections;
using System.ServiceModel;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Linq;
namespace LanSunERP
{ 
    [ServiceContract]
    interface IDataBaseLayerService
    {
        [OperationContract]
        DataSet GetDataSetByQuery(DataSet RequestDataSet);
        [OperationContract]
        DataSet RequestAgentStart(string senderAgent, string serviceChannel);
    }
    [ServiceBehavior(
        InstanceContextMode = InstanceContextMode.PerSession)]
    public class DataBaseLayerService : IDataBaseLayerService
    {
        public log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private DataSet DataSetServiceList;
        private DataSet dsReturn;
        private DataTable dtBaseTable;
        private DataTable dtRequest;
        private DataRow dwRequest;
        private string RequestJob;
        private SqlCommand cmdBuildingSQL;
        private SqlCommand cmdSQL;
        SqlConnection myConnection;
        SqlDataAdapter tableadapter;
        private string strtable_name_list = "";
        string strServiceType;
        string AgentID;
        string agentStatus;
        string mySelectQuery;
 
        DataRow[] dwServiceLists = null;
        DataRow dwServiceList = null;
        public DataBaseLayerService()
        {
            dsReturn = new DataSet();
            DataSetServiceList = new DataSet();
            myConnection = new SqlConnection("Data Source=SUNLIGHT;"
           + "Initial Catalog=LANSUNERP;Persist Security Info=True;User ID=LanSunERP2010;Password=LanSunERP2010");
            mySelectQuery = "SELECT * FROM ServiceList";
            tableadapter = new SqlDataAdapter(mySelectQuery, myConnection);
            tableadapter.Fill(DataSetServiceList, "ServiceList");

        }
        public DataSet GetDataSetByQuery(DataSet setRequest)
        {
            dsReturn = new DataSet();
            try
            {
           
                dtBaseTable = setRequest.Tables["AgentList"];
                dtRequest = setRequest.Tables["RequestTable"];
                AgentID = dtBaseTable.Rows[0]["AgentId"].ToString();
                agentStatus = dtBaseTable.Rows[0]["Status"].ToString();
                RequestJob = dtBaseTable.Rows[0]["LastRequestService"].ToString(); 
                 dwServiceLists = DataSetServiceList.Tables["ServiceList"].Select("ServiceID='" + RequestJob + "'");
                 if (dwServiceLists.Count() > 0)
                 {
                     dwServiceList = dwServiceLists[0];
                     ArrayList cmdArray = BuildcmdSQL(dwServiceList);
                     string strSQL = (string)cmdArray[0];
                     string strServiceTargetTable = (string)cmdArray[1];
                     string strtable_name_list = (string)cmdArray[2];

                     cmdSQL = new SqlCommand(strSQL, myConnection);

                     cmdSQL = BuildSQLParameters(cmdSQL, strServiceTargetTable);

                     tableadapter = new SqlDataAdapter(cmdSQL);
                     tableadapter.Fill(dsReturn, strServiceTargetTable);
                     dsReturn = NameTable(cmdArray, dsReturn, strtable_name_list);
                 }
                 else
                 { 
                 }
            }
            catch (Exception ex)
            {
                m_log.Error(ex);
            }

            return dsReturn;
        }
        private ArrayList BuildcmdSQL(DataRow dwServiceList)
        {
            string strServiceContent, strServicName, strServiceConditions, strServiceTargetTable, strServiceTargetTableID, strSQL;
            ArrayList cmdArray = new ArrayList(3);
            try
            {
                strServiceContent = dwServiceList["Contents"].ToString().TrimEnd();
                strServicName = dwServiceList["Name"].ToString().TrimEnd();
                strServiceConditions = dwServiceList["Conditions"].ToString().TrimEnd();
                strServiceType = dwServiceList["Type"].ToString().TrimEnd();
                strServiceTargetTable = dwServiceList["TableName"].ToString().TrimEnd();
                strServiceTargetTableID = dwServiceList["TableID"].ToString().TrimEnd();
                string strSortby = dwServiceList["Sortby"].ToString().TrimEnd(); 
       
                cmdBuildingSQL = new SqlCommand(strServiceContent, myConnection);
                cmdBuildingSQL.Parameters.Add(new SqlParameter("@table_name", strServiceTargetTable));
                cmdBuildingSQL.Parameters.Add(new SqlParameter("@type", strServiceType));
                cmdBuildingSQL.Parameters.Add(new SqlParameter("@conditions", strServiceConditions));
                cmdBuildingSQL.Parameters.Add(new SqlParameter("@Sortby", strSortby));
                cmdBuildingSQL.Parameters.Add(new SqlParameter("@resultsql_1", SqlDbType.NVarChar, -1));
                cmdBuildingSQL.Parameters.Add(new SqlParameter("@resultsql_2", SqlDbType.NVarChar, -1)); 
                cmdBuildingSQL.Parameters["@resultsql_1"].Direction = ParameterDirection.Output;
                cmdBuildingSQL.Parameters["@resultsql_2"].Direction = ParameterDirection.Output;

                tableadapter = new SqlDataAdapter(cmdBuildingSQL);
                tableadapter.Fill(DataSetServiceList, "CommandBuildingTable");

                strSQL = (String)cmdBuildingSQL.Parameters["@resultsql_1"].Value;
                strtable_name_list = (string)cmdBuildingSQL.Parameters["@resultsql_2"].Value;
                cmdArray.Add(strSQL);
                cmdArray.Add(strServiceTargetTable);
                cmdArray.Add(strtable_name_list);
                cmdArray.Add(strServiceTargetTableID);
                cmdArray.Add(strServiceConditions);
            }
            catch (Exception ex)
            {
                m_log.Error(ex);
            }
            return cmdArray;
        }
        private DataSet NameTable(ArrayList cmdArray, DataSet dsSource, string strtable_name_list)
        {
            string strServiceConditions, strServiceTargetTable, strServiceTargetTableID;
            strServiceTargetTableID = (string)cmdArray[3];
            strServiceConditions = (string)cmdArray[4];

            try
            {
 
                int strPosX = 0;
                int strPosY = 0;
                int i = 0;
                int iTmp = 0;

                if (dsSource.Tables.Count > 0)
                {
                    while (strtable_name_list != "")
                    {
                        iTmp = strtable_name_list.Length;
                        strPosX = strtable_name_list.IndexOf(";");
                        strServiceTargetTable = strtable_name_list.Substring(0, strPosX + 1);
                        strtable_name_list = strtable_name_list.Substring(strPosX + 1, iTmp - (strPosX + 1));
                        //strtable_name_list = strtable_name_list.Replace(strServiceTargetTable, "");
                        strServiceTargetTable = strServiceTargetTable.Replace(";", "");

                        strPosY = strServiceConditions.IndexOf(";");
                        if (strPosY == -1)
                        {
                            strServiceConditions = strServiceTargetTableID + ";";
                            strPosY = strServiceConditions.IndexOf(";");
                        }
                        strServiceTargetTableID = strServiceConditions.Substring(0, strPosY + 1);
                        strServiceConditions = strServiceConditions.Replace(strServiceTargetTableID, "");
                        strServiceTargetTableID = strServiceTargetTableID.Replace(";", "");

                        if (dsReturn.Tables[i] != null)
                        {
                            dsSource.Tables[i].TableName = strServiceTargetTable;
                            dsSource.Tables[i].DisplayExpression = strServiceTargetTableID;
                            dsSource.Tables[i].ExtendedProperties.Add("MakeUp", "NO");
                        }
                        i++;
                    }
                } 
            }
            catch (Exception ex)
            {
                m_log.Error(ex);
            }
            return dsSource;
        }
        private SqlCommand BuildSQLParameters(SqlCommand cmdSQL, string strServiceTargetTable)
        {
            switch (strServiceTargetTable.Trim())
            {
                case "ArticleGroup":
                    ArticleGroup m_ArticleGroup = new ArticleGroup(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ArticleGroup.BuildCommand();
                    break;
                case "ProductGroup":
                    ProductGroup m_ProductGroup = new ProductGroup(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ProductGroup.BuildCommand();
                    break;
                case "SizeMatrix":
                    SizeMatrix m_SizeMatrix = new SizeMatrix(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_SizeMatrix.BuildCommand();
                    break;
                case "SizeGroup":
                    SizeGroup m_SizeGroup = new SizeGroup(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_SizeGroup.BuildCommand();
                    break;
                case "FASHIONMASTER":
                    FASHIONMASTER m_FASHIONMASTER = new FASHIONMASTER(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_FASHIONMASTER.BuildCommand();
                    break;
                case "SystemDictionary":
                    SystemDictionary m_SystemDictionary = new SystemDictionary(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_SystemDictionary.BuildCommand();
                    break;
                case "DataSetConclusion":
                    DataSetConclusion m_DataSetConclusion = new DataSetConclusion(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_DataSetConclusion.BuildCommand();
                    break;
                case "UserManagement":
                    UserManagement m_UserManagement = new UserManagement(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_UserManagement.BuildCommand();
                    break;
                case "SystemManagement":
                    SystemManagement m_SystemManagement = new SystemManagement(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_SystemManagement.BuildCommand();
                    break;
                case "AgentList":
                    AgentList m_AgentList = new AgentList(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_AgentList.BuildCommand();
                    break;

                case "GUIProfile":
                    GUIProfile m_GUIProfile = new GUIProfile(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GUIProfile.BuildCommand();
                    break;
                case "TableColumnExtenedProperty":
                    TableColumnExtenedProperty m_TableColumnExtenedProperty = new TableColumnExtenedProperty(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_TableColumnExtenedProperty.BuildCommand();
                    break;
                case "GUIDesignTime":
                    GUIDesignTime m_GUIDesignTime = new GUIDesignTime(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GUIDesignTime.BuildCommand();
                    break;
                case "TableList2Agent":
                    TableList2Agent m_TableList2Agent = new TableList2Agent(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_TableList2Agent.BuildCommand();
                    break;
                case "ForeignKeyList":
                    ForeignKeyList m_ForeignKeyList = new ForeignKeyList(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ForeignKeyList.BuildCommand();
                    break;
                case "MachineControl":
                    MachineControl m_MachineControl = new MachineControl(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_MachineControl.BuildCommand();
                    break;
                case "MachineModel":
                    MachineModel m_MachineModel = new MachineModel(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_MachineModel.BuildCommand();
                    break;
                case "MachineNumber":
                    MachineNumber m_MachineNumber = new MachineNumber(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_MachineNumber.BuildCommand();
                    break;
                case "MachinePart":
                    MachinePart m_MachinePart = new MachinePart(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_MachinePart.BuildCommand();
                    break;
                case "MachineType":
                    MachineType m_MachineType = new MachineType(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_MachineType.BuildCommand();
                    break;
                case "MachineSettingDetail":
                    MachineSettingDetail m_MachineSettingDetail = new MachineSettingDetail(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_MachineSettingDetail.BuildCommand();
                    break;
                case "MachineSettingSet":
                    MachineSettingSet m_MachineSettingSet = new MachineSettingSet(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_MachineSettingSet.BuildCommand();
                    break;
                case "MachineSettingType":
                    MachineSettingType m_MachineSettingType = new MachineSettingType(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_MachineSettingType.BuildCommand();
                    break;
                case "ProcessFlowStepTechnicalSets":
                    ProcessFlowStepTechnicalSets m_ProcessFlowStepTechnicalSets = new ProcessFlowStepTechnicalSets(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ProcessFlowStepTechnicalSets.BuildCommand();
                    break;
                case "ProcessPointTechnicalSets":
                    ProcessPointTechnicalSets m_ProcessPointTechnicalSets = new ProcessPointTechnicalSets(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ProcessPointTechnicalSets.BuildCommand();
                    break;
                case "ProcessFlow":
                    ProcessFlow m_ProcessFlow = new ProcessFlow(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ProcessFlow.BuildCommand();
                    break;
                case "ProcessFlowHead":
                    ProcessFlowHead m_ProcessFlowHead = new ProcessFlowHead(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ProcessFlowHead.BuildCommand();
                    break;
                case "ProcessFlowStep":
                    ProcessFlowStep m_ProcessFlowStep = new ProcessFlowStep(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ProcessFlowStep.BuildCommand();
                    break;
                case "GoodsTypeKeyDefRelation":
                    GoodsTypeKeyDefRelation m_GoodsTypeKeyDefRelation =
                       new GoodsTypeKeyDefRelation(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsTypeKeyDefRelation.BuildCommand();
                    break;
                case "TraceInfor":
                    TraceInfor m_TraceInfor = new TraceInfor(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_TraceInfor.BuildCommand();
                    break;
                case "MovementList":
                    MovementList m_MovementList = new MovementList(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_MovementList.BuildCommand();
                    break;
                case "ProcessPointAssemblePoint":
                    ProcessPointAssemblePoint m_ProcessPointAssemblePoint = new ProcessPointAssemblePoint(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ProcessPointAssemblePoint.BuildCommand();
                    break;
                case "GoodsTypeAssemblePointDef":
                    GoodsTypeAssemblePointDef m_GoodsTypeAssemblePointDef = new GoodsTypeAssemblePointDef(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsTypeAssemblePointDef.BuildCommand();
                    break;

                case "SysKeyData":
                    SysKeyData m_SysKeyData = new SysKeyData(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_SysKeyData.BuildCommand();
                    break;

                case "OperationKey":
                    OperationKey m_OperationKey = new OperationKey(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_OperationKey.BuildCommand();
                    break; 

                case "MenuBuild":
                    MenuBuild MenuBuild = new MenuBuild(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = MenuBuild.BuildCommand();
                    break;
                case "GoodsMastCreatCond":
                    GoodsMastCreatCond m_GoodsMastCreatCond = new GoodsMastCreatCond(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsMastCreatCond.BuildCommand();
                    break; 
                case "GoodsMaster_Key_Relation":
                    GoodsMaster_Key_Relation m_GoodsMaster_Key_Relation = new GoodsMaster_Key_Relation(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsMaster_Key_Relation.BuildCommand();
                    break;

                case "UserRightRules":
                    UserRightRules m_UserRightRules = new UserRightRules(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_UserRightRules.BuildCommand();
                    break;
                case "UserRightManagement":
                    UserRightManagement m_UserRightManagement = new UserRightManagement(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_UserRightManagement.BuildCommand();
                    break;
                case "ApplicationManagement":
                    ApplicationManagement m_ApplicationManagement = new ApplicationManagement(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ApplicationManagement.BuildCommand();
                    break;
                case "PlanningTaskQueryConditions":
                    PlanningTaskQueryConditions m_PlanningTaskQueryConditions = new PlanningTaskQueryConditions(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_PlanningTaskQueryConditions.BuildCommand();
                    break;
                case "AssemblingInstanceGoodsFullFillList":
                    AssemblingInstanceGoodsFullFillList m_AssemblingInstanceGoodsFullFillList = new AssemblingInstanceGoodsFullFillList(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_AssemblingInstanceGoodsFullFillList.BuildCommand();
                    break;

                case "GoodsMasterAssemblePoint":
                    GoodsMasterAssemblePoint m_GoodsMasterAssemblePoint = new GoodsMasterAssemblePoint(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsMasterAssemblePoint.BuildCommand();
                    break;
                case "ProcessPoint":
                    ProcessPoint m_ProcessPoint = new ProcessPoint(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ProcessPoint.BuildCommand();
                    break;
                case "GoodsAssemblePointType":
                    GoodsAssemblePointType m_GoodsAssemblePointType = new GoodsAssemblePointType(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsAssemblePointType.BuildCommand();
                    break;
                case "GoodsMasterKey":
                    GoodsMasterKey m_GoodsMasterKey = new GoodsMasterKey(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsMasterKey.BuildCommand();
                    break;
                case "GoodsTypeKeyDef":
                    GoodsTypeKeyDef m_GoodsTypeKeyDef = new GoodsTypeKeyDef(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsTypeKeyDef.BuildCommand();
                    break;
                case "GoodsMasterAssemblePointList":
                    GoodsMasterAssemblePointList m_GoodsMasterAssemblePointList = new GoodsMasterAssemblePointList(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsMasterAssemblePointList.BuildCommand();
                    break;
                case "GoodsMasterData":
                    GoodsMasterData m_GoodsMasterData = new GoodsMasterData(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsMasterData.BuildCommand();
                    break;
                case "GoodsMoveMentType":
                    GoodsMoveMentType m_GoodsMoveMentType = new GoodsMoveMentType(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsMoveMentType.BuildCommand();
                    break;

                case "Fabric_Base":
                    Fabric_Base m_Fabric_Base = new Fabric_Base(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_Fabric_Base.BuildCommand();
                    break;
                case "Weave_Base":
                    Weave_Base m_Weave_Base = new Weave_Base(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_Weave_Base.BuildCommand();
                    break;
                case "ColorWay_Base":
                    ColorWay_Base ColorWay_Base = new ColorWay_Base(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = ColorWay_Base.BuildCommand();
                    break;

                case "GoodsTypePropertyDef":
                    GoodsTypePropertyDef m_GoodsTypePropertyDef = new GoodsTypePropertyDef(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsTypePropertyDef.BuildCommand();
                    break;
                case "GoodsMasterProperty":
                    GoodsMasterProperty m_GoodsMasterProperty = new GoodsMasterProperty(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsMasterProperty.BuildCommand();
                    break;
                case "PropertyList":
                    PropertyList m_PropertyList = new PropertyList(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_PropertyList.BuildCommand();
                    break;
                case "SalesOrderLine":
                    SalesOrderLine m_SalesOrderLine = new SalesOrderLine(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_SalesOrderLine.BuildCommand();
                    break;
                case "SalesOrder":
                    SalesOrder m_SalesOrder = new SalesOrder(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_SalesOrder.BuildCommand();
                    break;
                case "CustomerMasterData":
                    CustomerMasterData m_CustomerMasterData = new CustomerMasterData(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_CustomerMasterData.BuildCommand();
                    break;
                case "ProdOrderLine":
                    ProdOrderLine m_ProdOrderLine = new ProdOrderLine(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ProdOrderLine.BuildCommand();
                    break;
                case "ProdOrder":
                    ProdOrder m_ProdOrder = new ProdOrder(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ProdOrder.BuildCommand();
                    break;
                case "ProdOrderProcessFlowStep":
                    ProdOrderProcessFlowStep m_ProdOrderProcessFlowStep = new ProdOrderProcessFlowStep(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ProdOrderProcessFlowStep.BuildCommand();
                    break;
                case "GoodsInventory":
                    GoodsInventory m_GoodsInventory = new GoodsInventory(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsInventory.BuildCommand();
                    break;
                case "GoodsStock":
                    GoodsStock m_GoodsStock = new GoodsStock(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsStock.BuildCommand();
                    break;
                case "GoodsMoveMentRecord":
                    GoodsMoveMentRecord m_GoodsMoveMentRecord = new GoodsMoveMentRecord(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsMoveMentRecord.BuildCommand();
                    break;
                case "GoodsWareHouse":
                    GoodsWareHouse m_GoodsWareHouse = new GoodsWareHouse(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsWareHouse.BuildCommand();
                    break;
                case "AssemblingInstanceGoodsUsageList":
                    AssemblingInstanceGoodsUsageList m_AssemblingInstanceGoodsUsageList = new AssemblingInstanceGoodsUsageList(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_AssemblingInstanceGoodsUsageList.BuildCommand();
                    break;
                case "AssemblingInstanceGoodsList":
                    AssemblingInstanceGoodsList m_AssemblingInstanceGoodsList = new AssemblingInstanceGoodsList(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_AssemblingInstanceGoodsList.BuildCommand();
                    break;
                case "CostCenter":
                    CostCenter m_CostCenter = new CostCenter(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_CostCenter.BuildCommand();
                    break;
                case "CostCenterQueryCond":
                    CostCenterQueryCond m_CostCenterQueryCond =
                        new CostCenterQueryCond(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_CostCenterQueryCond.BuildCommand();
                    break;
                case "CostCenterOperation":
                    CostCenterOperation m_CostCenterOperation =
                        new CostCenterOperation(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_CostCenterOperation.BuildCommand();
                    break;
                case "CostCenterOperationQueryCond":
                    CostCenterOperationQueryCond m_CostCenterOperationQueryCond =
                        new CostCenterOperationQueryCond(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_CostCenterOperationQueryCond.BuildCommand();
                    break;

                case "ServiceList":
                    ServiceList m_ServiceList = new ServiceList(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_ServiceList.BuildCommand();
                    break;
                case "GoodsType":
                    GoodsType m_GoodsType = new GoodsType(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsType.BuildCommand();
                    break;
                case "GoodsTypeQueryCond":
                    GoodsTypeQueryCond m_GoodsTypeQueryCond = new GoodsTypeQueryCond(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_GoodsTypeQueryCond.BuildCommand();
                    break;
                case "TableList":
                    TableList m_TableList = new TableList(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_TableList.BuildCommand();
                    break;
                case "PropertyGroup":
                    PropertyGroup m_PropertyGroup = new PropertyGroup(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_PropertyGroup.BuildCommand();
                    break;
                case "Multi_Table":
                    Multi_Table m_Multi_Table = new Multi_Table(cmdSQL, dtRequest, RequestJob);
                    cmdSQL = m_Multi_Table.BuildCommand();
                    break;

                default:
                     m_log.Warn("DataBaseService table: " + strServiceTargetTable+"Missed!");
                    break;

            }
            System.DateTime dtTime = System.DateTime.Now;
            if (!cmdSQL.Parameters.Contains("@FrstUser"))
            {
                cmdSQL.Parameters.Add(new SqlParameter("@FrstUser", "LANSUN"));
                cmdSQL.Parameters.Add(new SqlParameter("@FrstDate", dtTime));
                cmdSQL.Parameters.Add(new SqlParameter("@LastUser", "LANSUN"));
                cmdSQL.Parameters.Add(new SqlParameter("@LastDate", dtTime));
            }
            WriteLog(cmdSQL, "QueryString", agentStatus);
            return cmdSQL;
        }
        public DataSet RequestAgentStart(string senderAgent, string serviceChannel)
        {
            DataSet resultDS = new DataSet();
            try
            {
                string mySelectQuery = "SELECT top 1 * FROM AgentList where  AgentID='" + senderAgent + "'";

                SqlConnection myConnection = new SqlConnection("Data Source=SUNLIGHT;Initial Catalog=LANSUNERP;Persist Security Info=True;User ID=LanSunERP2010;Password=LanSunERP2010");
                SqlDataAdapter tableadapter = new SqlDataAdapter(mySelectQuery, myConnection);

                //DataTable dtTmp = new DataTable();
                tableadapter.Fill(resultDS, "AgentList");
                resultDS.Tables["AgentList"].DisplayExpression = "100044";
                mySelectQuery = "SELECT top 1 * FROM ServiceList where AgentID='" + senderAgent + "'";
                tableadapter = new SqlDataAdapter(mySelectQuery, myConnection);
                tableadapter.Fill(resultDS, "ServiceList");
                resultDS.Tables["ServiceList"].DisplayExpression = "100000";
            }
            catch (Exception ex)
            {
                m_log.Error(ex);
            }
            //ds.Tables.Add(dtTmp);
            return resultDS;
        }
        public void WriteLog(object sender, string information, string agentStatus)
        {
            if (agentStatus == "Trace")
            {
                switch (information)
                {
                    case "QueryString":
                        SqlCommand cmdSql = (SqlCommand)sender;
                        m_log.Warn(cmdSql.CommandType + ":");
                        m_log.Warn(cmdSql.CommandText + ": With Paramaters");
                        foreach (SqlParameter sp in cmdSql.Parameters)
                        {
                            m_log.Warn("Paramater:" + sp.ParameterName + ", Value: " + sp.Value.ToString());
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
}